<?php
/**
 * YAPP Base View class
 *
 */

class View
{

    private $parser;
    protected $defaultDir;
    protected $controller;

    // Link controller to view
    public function linkController(&$controller)
    {
        $this->controller = $controller;
    }

    // Instance view class
    public function __construct($templatesConf, $moduleTemplatesDir)
    {
        if (!empty($templatesConf['cache_dir']))
        {
            if (!file_exists($templatesConf['cache_dir']))
            {
                die("Templates cache dir does not exists");
            }
        }
        else
        {
            die("Templates cache dir is not specified!");
        }
        if (!file_exists($moduleTemplatesDir))
        {
            die("Modules templates dir does not exists");
        }
        $this->parser = new yappTPL($templatesConf['cache_dir'], $moduleTemplatesDir, $templatesConf['cache']);
        // Default
        if (empty($templatesConf['default_dir']))
        {
            die("No default dir selected!");
        }
        else
        {
            $this->defaultDir = $templatesConf['default_dir'];
        }
    }

    // Set template
    public function setTemplate($template, $directory = false)
    {
        $this->parser->setTemplate($template, $directory);
    }

    // Parse template
    public function parse($template, $vars = array(), $templateFile = false, $templateDirectory = false)
    {
        $this->parser->parse($template, $vars, $templateFile = false, $templateDirectory = false);
    }

    // Build pagination string from pagination standard results
    public function buildPagination($link, &$paginationVars)
    {
        if (!empty($paginationVars['totalPages']) && ($paginationVars['totalPages'] > 1))
        {
            $retHtml = '<div class="pagination">';
            if ($paginationVars['page'] != 1)
            {
                $link = $link.(strstr($link,'?')?'&':'?').'page='.($paginationVars['page']-1);
                $retHtml .= '<a href="'.$link.'">&#60;&#60;</a>&nbsp;';
            }
            for ($i=1;$i<=$paginationVars['totalPages'];$i++)
            {
                if ($i > 1)
                {
                    $retHtml .= '&nbsp;|&nbsp;';
                }
                $retHtml .= '<span class="page_number">';
                if ($i == $paginationVars['page'])
                {
                    $retHtml .= $i;
                }
                else
                {
                    $linkP = $link.(strstr($link,'?')?'&':'?').'page='.$i;
                    $retHtml .= '<a href="'.$linkP.'">'.$i.'</a>';
                }
            }
            if ($paginationVars['page'] != $paginationVars['totalPages'])
            {
                $linkP = $link.(strstr($link,'?')?'&':'?').'page='.($paginationVars['page']+1);
                $retHtml .= '&nbsp;<a href="'.$linkP.'">&#62;&#62;</a>';
            }
            $retHtml .= '</div>';
            return $retHtml;
        }
        else
        {
            return '';
        }
    }

}
?>